import Vue from "vue";
import axios from "axios";
import api from '../api';
import SQL from './sql'

Vue.prototype.$http = axios
Vue.prototype.$sql = SQL
axios.defaults.headers.post['Content-Type'] = 'application/json; charset=UTF-8'
Vue.prototype.$request = {
  itemStart: function (ok, fail, date) {
    try {
      const data = {};
      data.api = api.startItem;
      data.data = this.$getBase();
      data.data.kssj = date;
        this.$http.post(this.$store.getters.uri, data).then(res => ok.call(this, res)).catch(err => fail.call(this, err));
    } catch (e) {
      fail(e);
    }
  },
  takePhoto: function (ok, fail, sbdh) {
    try {
      const data = this.$getBase();
      const param = {};
      param.api = api.takePhoto;
      data.sbdh = sbdh;
      param.data = data;
      this.$http.post(this.$store.getters.uri, param).then(res => ok(res)).catch(err => fail(err));
    } catch (e) {
      fail(e);
    }
  },
  itemEnd: function (ok, fail, date) {
    try {
      const data = {};
      data.api = api.stopItem;
      data.data = this.$getBase();
      data.data.jssj = date;
      this.$http.post(this.$store.getters.uri, data).then(res => ok(res)).catch(err => fail(err));
    } catch (e) {
      fail(e);
    }
  },
  connDb: function (ok, fail, sql,date) {
    try {
      const data = {};
      data.api = 404;
      data.data = {sql: sql.call(this, date)}
      this.$http.post(this.$store.getters.uri, data)
        .then(ok)
        .catch(fail);
    } catch (e) {
      fail(e);
    }

  }
}
